
	import {
		v6 as uuidv6
	} from 'uuid';
	import {
		get_upload_token,
		upload as uploadApi
	} from '@/api/upload'
		//  获取上传token
	function getUploadToken() {

		return new Promise(async function(resolve, reject) {

			let uploadToken = localStorage.getItem('uploadToken')
			let uploadTokenTime = localStorage.getItem('uploadTokenTime') || 0;

			let now = new Date().getTime()

			if (uploadToken && uploadTokenTime > now) {
				//  如果有老token  并且没过期  
				resolve(uploadToken)
			} else {

				let {
					token
				} = await get_upload_token();
				localStorage.setItem('uploadToken', token)
				localStorage.setItem('uploadTokenTime', now + 6 * 60 * 60 * 1000)

				resolve(token)

			}


		})

	}

	//  随机文件名 

	export async function upload(file) {
		try {
			const formdata = new FormData()
			let UploadToke = await getUploadToken()
			formdata.append('token', UploadToke)
			formdata.append('file', file)
			formdata.append('key', uuidv6())
			return uploadApi(formdata)

			// resolve(import.meta.env.VITE_IMG_URL + key)
		} catch (err) {

		}

	}